package de.jmda.gen.java.impl;

import java.io.Serializable;

import org.apache.log4j.Logger;
import org.junit.Test;

import de.jmda.gen.GeneratorException;
import de.jmda.gen.LineIndenter;
import de.jmda.gen.java.ClassGenerator;
import de.jmda.gen.java.DeclaredFieldGenerator;
import de.jmda.gen.java.InstanceFieldDeclarationGenerator;
import de.jmda.gen.java.naming.ImportManager;
import de.jmda.gen.java.naming.ImportManagerProvider;

public class JUT_ManagedImportStatementsGenerator
{
	private final static Logger LOGGER =
      Logger.getLogger(JUT_ManagedImportStatementsGenerator.class);

	@Test
	public void testManagedImportStatementsGenerator()
			throws GeneratorException
	{
		ClassGenerator generator = new DefaultClassGenerator();

		String packagename = getClass().getPackage().getName();

		ImportManager importManager =
				ImportManagerProvider.demandImportManager(packagename);

		generator.setPackageName(packagename);
		generator.setImportStatementsGenerator(
				new ManagedImportStatementsGenerator(importManager));
		generator.setImplementsClause(importManager.useType(Serializable.class));
		generator.addInstanceFieldGenerator(getInstanceDateFieldGenerator1());
		generator.addInstanceFieldGenerator(getInstanceDateFieldGenerator2());

		StringBuffer generated = generator.generate();

		LOGGER.debug("generated [" + generated + "]");
	}

	private InstanceFieldDeclarationGenerator getInstanceDateFieldGenerator1()
  {
		InstanceFieldDeclarationGenerator result =
				new DefaultInstanceFieldDeclarationGenerator();

		DeclaredFieldGenerator generator =
				new DefaultDeclaredInstanceFieldGenerator();

		generator.setTypeNameGenerator(
				new DefaultTypeNameGenerator(
						ImportManagerProvider
								.demandImportManager()
								.useType(java.util.Date.class)));

		result.setLineIndenter(LineIndenter.TAB_SINGLE);
		result.setDeclaredFieldGenerator(generator);
		
	  return result;
  }

	private InstanceFieldDeclarationGenerator getInstanceDateFieldGenerator2()
  {
		InstanceFieldDeclarationGenerator result =
				new DefaultInstanceFieldDeclarationGenerator();
		
		DeclaredFieldGenerator generator =
				new DefaultDeclaredInstanceFieldGenerator();

		generator.setTypeNameGenerator(
				new DefaultTypeNameGenerator(
						ImportManagerProvider
								.demandImportManager()
								.useType(java.sql.Date.class)));

		result.setLineIndenter(LineIndenter.TAB_SINGLE);
		result.setDeclaredFieldGenerator(generator);
		
	  return result;
  }
}